package org.zgy.stock.threads.holder;

import com.alibaba.fastjson.JSONObject;
import lombok.val;
import org.zgy.stock.model.holder.StockMainHolderInfo;
import org.zgy.stock.service.holder.StockMainHolderInfoService;
import org.zgy.stock.utils.okhttp3.OkHttpUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

public class StockMainHolderInfoThread implements Runnable {

    private static final String API = "https://data.eastmoney.com/dataapi/zlsj/detail?SHType=0&SHCode=&SCode=STOCK_CODE_WITH_MARKET&ReportDate=STOCK_DATE_WITH_MARKET&sortField=HOLDER_CODE&sortDirec=1&pageNum=STOCK_PAGE_NUMBER&pageSize=100";
    private final String code;
    private final StockMainHolderInfoService stockMainHolderInfoService;

    public StockMainHolderInfoThread(String code, StockMainHolderInfoService stockMainHolderInfoService) {
        this.code = code;
        this.stockMainHolderInfoService = stockMainHolderInfoService;
    }

    @Override
    public void run() {
        int year = 2025;

        int pageNumber_05 = 1;
        String stockDate_25 = year + "-03-31";
        int totalPage_25 = fetchData(pageNumber_05, stockDate_25);
        for (int i = 2; i <= totalPage_25; i++) {
            fetchData(i, stockDate_25);
        }
        year = 2024;

        while (true) {
            String[] dateList = {"12-31", "09-30", "06-30", "03-31"};
            boolean isContinue = true;
            for (String date : dateList) {
                int pageNumber = 1;
                String stockDate = year + "-" + date;
                int totalPage = fetchData(pageNumber, stockDate);
                if (totalPage == -1) {
                    isContinue = false;
                    break;
                }
                for (int i = 2; i <= totalPage; i++) {
                    fetchData(i, stockDate);
                }
            }
            year = year - 1;
            if (!isContinue) {
                break;
            }
            if (year < 2005) {
                break;
            }
        }



    }

    private int fetchData(int pageNumber, String stockDate) {
        try {
            String result = OkHttpUtil.getRequest(API.replace("STOCK_CODE_WITH_MARKET", code)
                    .replace("STOCK_DATE_WITH_MARKET", stockDate)
                    .replace("STOCK_PAGE_NUMBER", String.valueOf(pageNumber)), null);
            if (Objects.equals(result, "[]")) {
                return -1;
            }
            if (result != null && !result.isEmpty()) {
                JSONObject resultJson = JSONObject.parseObject(result);
                int totalPage = resultJson.getInteger("pages");
                val list = new ArrayList<>(JSONObject.parseArray(resultJson.getString("data"), StockMainHolderInfo.class));
                if (!list.isEmpty()) {
                    stockMainHolderInfoService.insert(list);
                }
                return totalPage;
            }
        }catch (Exception exception) {
            exception.printStackTrace();
        }

        return 0;
    }
}
